package com.feng.demo.gui;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Icon;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

public class ActiveGUITest {
	private JTextArea messageTextArea = null;
	private JFormattedTextField userField = null;
	private JFormattedTextField statusField = null;
	private DefaultTableModel messagesTable = null;
	JTabbedPane tabbedPane;
	private JFrame frame;

	private void init() {
		createDebug();
		JSplitPane allPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		allPane.setOneTouchExpandable(true);

		messagesTable = new DefaultTableModel(new Object[] { "Hide", "Timestamp", "", "", "Message", "Id", "Type", "To", "From" }, 0) {
			private static final long serialVersionUID = 8136121224474217264L;

			public boolean isCellEditable(int rowIndex, int mColIndex) {
				return false;
			}

			public Class getColumnClass(int columnIndex) {
				if (columnIndex == 2 || columnIndex == 3) {
					return Icon.class;
				}
				return super.getColumnClass(columnIndex);
			}

		};

		JTable table = new JTable(messagesTable);
		// Allow only single a selection
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		// Hide the first column
		table.getColumnModel().getColumn(0).setMaxWidth(0);
		table.getColumnModel().getColumn(0).setMinWidth(0);
		table.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
		table.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
		// Set the column "timestamp" size
		table.getColumnModel().getColumn(1).setMaxWidth(300);
		table.getColumnModel().getColumn(1).setPreferredWidth(70);
		// Set the column "direction" icon size
		table.getColumnModel().getColumn(2).setMaxWidth(50);
		table.getColumnModel().getColumn(2).setPreferredWidth(30);
		// Set the column "packet type" icon size
		table.getColumnModel().getColumn(3).setMaxWidth(50);
		table.getColumnModel().getColumn(3).setPreferredWidth(30);
		// Set the column "Id" size
		table.getColumnModel().getColumn(5).setMaxWidth(100);
		table.getColumnModel().getColumn(5).setPreferredWidth(55);
		// Set the column "type" size
		table.getColumnModel().getColumn(6).setMaxWidth(200);
		table.getColumnModel().getColumn(6).setPreferredWidth(50);
		// Set the column "to" size
		table.getColumnModel().getColumn(7).setMaxWidth(300);
		table.getColumnModel().getColumn(7).setPreferredWidth(90);
		// Set the column "from" size
		table.getColumnModel().getColumn(8).setMaxWidth(300);
		table.getColumnModel().getColumn(8).setPreferredWidth(90);
		// Create a table listener that listen for row selection events
		SelectionListener selectionListener = new SelectionListener(table);
		table.getSelectionModel().addListSelectionListener(selectionListener);
		table.getColumnModel().getSelectionModel().addListSelectionListener(selectionListener);

		allPane.setTopComponent(new JScrollPane(table));

		allPane.setDividerLocation(150);

		tabbedPane.add("All Packets", allPane);
		tabbedPane.setToolTipTextAt(0, "Sent and received packets processed by Smack");

		final JTextArea sentText = new JTextArea();
		sentText.setWrapStyleWord(true);
		sentText.setLineWrap(true);
		sentText.setEditable(false);
		sentText.setForeground(new Color(112, 3, 3));
		tabbedPane.add("Raw Sent Packets", new JScrollPane(sentText));
		tabbedPane.setToolTipTextAt(0, "Raw text of the sent packets");

	}

	private void createDebug() {

		frame = new JFrame("Smack Debug Window");

		// Add listener for window closing event
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
			}
		});

		// We'll arrange the UI into tabs. The last tab contains Smack's
		// information.
		// All the connection debugger tabs will be shown before the Smack info
		// tab.
		tabbedPane = new JTabbedPane();
		frame.setTitle("Smack EnhancedDebugger Window -- Total connections: " + (tabbedPane.getComponentCount() - 1));
		frame.getContentPane().add(tabbedPane);

		frame.setSize(650, 400);

		frame.setVisible(true);
	}

	private class SelectionListener implements ListSelectionListener {

		JTable table;

		// It is necessary to keep the table since it is not possible
		// to determine the table from the event's source
		SelectionListener(JTable table) {
			this.table = table;
		}

		public void valueChanged(ListSelectionEvent e) {
			if (table.getSelectedRow() == -1) {
				// Clear the messageTextArea since there is none packet selected
				messageTextArea.setText(null);
			} else {
				// Set the detail of the packet in the messageTextArea
				messageTextArea.setText((String) table.getModel().getValueAt(table.getSelectedRow(), 0));
				// Scroll up to the top
				messageTextArea.setCaretPosition(0);
			}
		}
	}

	public static void main(String[] args) {
		final ActiveGUITest t = new ActiveGUITest();
		t.init();
		for (int i = 1; i < 10; i++) {
			System.out.println(i);
			try {
				Thread.currentThread().sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			final int j = i;
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					t.tabbedPane.setTitleAt(0, "test");
					final JTextArea sentText = new JTextArea();
					sentText.setWrapStyleWord(true);
					sentText.setLineWrap(true);
					sentText.setEditable(false);
					sentText.setForeground(new Color(112, 3, 3));
					t.tabbedPane.add("Raw Sent Packets", new JScrollPane(sentText));
					t.tabbedPane.setToolTipTextAt(j, "Raw text of the sent packets");
					t.frame.getContentPane().add(t.tabbedPane);
				}
			});
		}
	}
}
